import xml.dom.minidom

from com.power.auto.test.utils.down_file import *

_dx_name = ''
_download_top_base_url = 'http://artifact.paas.yp:8000/artifactory/yp3g-subsystem-snapshot/com/yeepay/power/%s/1.0-SNAPSHOT/'


def request_nexus():
    global r
    global _dx_name
    print(_dx_name)
    url = _download_top_base_url % _dx_name
    xml_url = url + 'maven-metadata.xml'
    print(url)
    r = requests.get(xml_url)
    print('result=%s' % r)
    tree = xml.dom.minidom.parseString(r)
    print(tree)
    return r


def get_jar_url_path(jar_name):
    base_url = _download_top_base_url % jar_name
    jar_url = (base_url) + 'maven-metadata.xml'
    real_name = download_file(jar_url, jar_name + ".xml")
    f_io = open(real_name, 'rb')
    doc = xml.dom.minidom.parseString(f_io.read())
    bulk_pm_mr_data_file = doc.documentElement
    enbs = bulk_pm_mr_data_file.getElementsByTagName('snapshotVersion')
    suf = enbs[1].getElementsByTagName('value')
    return base_url + jar_name + '-' + suf[0].childNodes[0].data + '.jar'


def push_jar(jar_name):
    _jar_url = get_jar_url_path(jar_name)
    download_file(_jar_url, jar_name + '.jar')


def init_maven():
    push_jar('power-facade')
    push_jar('power-common')
